<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<view>
			<uni-nav-bar left-icon="arrowleft" @clickLeft="back" title="我要代理" :statusBar='true' :fixed="true"></uni-nav-bar>
		</view>
		<!-- #endif -->
		<view class="mywantagent">
			<!-- #ifdef APP-PLUS -->
			<view class="advertisement" :style="{ 'height': appImgHeight + 'px' }">
			<!-- #endif -->
			<!-- #ifndef APP-PLUS -->
			<view class="advertisement" :style="{ 'height': h5ImgHeight + 'px' }">
			<!-- #endif -->
				<image :src="commonImg('agent1.jpg')"></image>
			</view>
			<view class="agent-btn">
				<view class="agent-btn-one">{{ countdown }}s后自动开始申请</view>
				<view @click="onClickGotoMywantagentapplyPage" class="agent-btn-two">立即开始申请</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		myMixin
	} from '@/utils/mixin.js'
	import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'

	export default {
		mixins: [myMixin],
		components: {
			uniNavBar
		},
		data() {
			return {
				appImgHeight: 0,
				h5ImgHeight: 0,
				timer: null,
				countdown: 5
			}
		},
		onReady() {
			const that = this
			uni.getSystemInfo({
				success: ((res) => {
					let info = uni.createSelectorQuery().select(".agent-btn");
					info.boundingClientRect(function(data) {
						that.h5ImgHeight = res.windowHeight - data.height // 小程序和H5是可以这么设置，但是到了自定义头部的时候，是不允许这么设置的
						that.appImgHeight = res.windowHeight - res.statusBarHeight - 44 - data.height // app的height
					}).exec()
				})
			})
		},
		onLoad() {
			this._beginSetTimer()
		},
		// 页面卸载的时候，一定要清除定时器
		onUnload() {
			if (this.timer) {
				this.countdown = 5
				clearTimeout(this.timer)
			}
		},
		methods: {
			back() {
				uni.navigateBack({})
			},
			_beginSetTimer() {
				if (this.countdown === 1) {
					uni.redirectTo({
						url: '../mywantagentapply/mywantagentapply'
					})
					this.countdown = 5
					clearTimeout(this.timer)
					this.timer = null
					return
				} else {
					this.countdown--
				}
				this.timer = setTimeout(() => {
					this._beginSetTimer()
				}, 1000)
			},
			onClickGotoMywantagentapplyPage() {
				uni.redirectTo({
					url: '../mywantagentapply/mywantagentapply'
				})
			}
		}
	}
</script>
<style>
	page {
		width: 100%;
	}
</style>
<style scoped>
	.mywantagent {
		display: flex;
		flex-direction: column;
	}

	.advertisement {
		width: 100%;
		display: block;
		background-color: #eee;
	}

	.advertisement image {
		width: 100%;
		height: 100%;
		display: block;
	}

	.agent-btn {
		height: 180rpx;
	}

	.agent-btn-one {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 28rpx;
		text-align: center;
		color: #000;
		border-bottom: 2rpx solid #ccc;
		box-sizing: border-box;
	}

	.agent-btn-two {
		height: 90rpx;
		line-height: 90rpx;
		font-size: 28rpx;
		text-align: center;
		color: #1677FF;
	}
</style>
